package org.vidi.springamqp.comsumer;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;
import org.vidi.springamqp.entity.Order;

import java.io.IOException;
import java.util.Map;

/**
 * @author vidi
 */
@Component
public class OrderConsumer {
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "ORDER_QUEUE", durable = "true"),
            exchange = @Exchange(name = "ORDER_EXCHANGE", type = "topic"),
            key = "order.*"
    ))
    @RabbitHandler
    public void onOrderMessage(@Payload Order order, @Headers Map<String, Object> headers, Channel channel) throws IOException {
        System.out.println("Start Receive Order");
        System.out.println("Order Id: " + order.getId() +
                ", Order Name: " + order.getName() +
                ", Order Message Id: " + order.getMessageId());

        Long deliveryTag = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);
        channel.basicAck(deliveryTag, false);
    }
}
